package java8.lambda.demo03;

import java8.lambda.demo01.AbstractAppleBeforeTest;
import java8.lambda.demo01.Apple;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

/**
 * @author ChangLiang
 * @date 2020/4/22
 */
public class AppTest extends AbstractAppleBeforeTest {

    @Test
    @DisplayName("test Predicate")
    public void test() {
        List<Apple> apple = AppleFilter.findApple(list, apple1 -> apple1.getColor().equals("green") && apple1.getWeight() > 150);
        assertThat(apple.size()).isEqualTo(1);
    }

    @Test
    @DisplayName("test long Predicate")
    public void test2() {
        List<Apple> apple = AppleFilter.findAppleByWeight(list, weight -> weight>150);
        assertThat(apple.size()).isEqualTo(1);
    }

    @Test
    @DisplayName("test BiPredicate")
    public void test3() {
        List<Apple> apple = AppleFilter.findAppleByColorAndWeight(list,(color,weight)->color.equals("green") && weight>150);
        assertThat(apple.size()).isEqualTo(1);
    }

    @Test
    @DisplayName("test Consumer")
    public void test4() {
        AppleConsumer.consumeAppleWithFor(list, apple-> System.out.println(apple));

        AppleConsumer.consumeAppleWithFor(list, System.out::println);
    }


    @Test
    @DisplayName("test Consumer")
    public void test5() {
        AppleConsumer.consumeAppleWithForeach(list, apple-> System.out.println(apple));
    }

    @Test
    @DisplayName("test BiConsumer")
    public void test6() {
        AppleConsumer.consumeAppleWithBiConsumer(list,(apple, str)->System.out.println(apple.getColor()+str),"-cl");
    }

    @Test
    @DisplayName("test BiConsumer")
    public void test7() {
        AppleConsumer.consumeAppleWithBiConsumerForeach(list,(apple, str)->System.out.println(apple.getColor()+str),"-cl");
    }

    @Test
    @DisplayName("test Function")
    public void test8() {
        Apple afterApple = AppleFunction.applyAppleFunction(list.get(0), (apple) -> {
            apple.setWeight(apple.getWeight() * 10);
            return apple;
        });
        System.out.println(list.get(0));
        // do change in origin list
        System.out.println(list.get(0).equals(afterApple));
    }


    @Test
    @DisplayName("test Function")
    public void test9() {
        list.forEach((apple)->{
            AppleFunction.applyAppleFunction(apple, (apple1) -> {
                apple1.setWeight(apple1.getWeight() * 10);
                return apple1;
            });
        });
        System.out.println(list);
    }

    @Test
    @DisplayName("test BiFunction")
    public void test10() {
        Apple apple = AppleFunction.applyAppleFunctionWithColorAndWeight("Blue", 130L, Apple::new);
        System.out.println(apple);
    }

    @Test
    @DisplayName("test Supplier")
    public void test11() {
        Apple apple = AppleSupplier.createApple(Apple::new);
        System.out.println(apple);
    }

}
